package edu.gatech.teamrocket;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.j256.ormlite.dao.Dao;

import edu.gatech.teamrocket.models.Auth;
import edu.gatech.teamrocket.models.Task;

/**
 * Displays the given list of tasks on a map.
 * @author Patrick
 *
 */
public class TaskMapActivity extends MapActivity {
	private DewApplication app;
	private Auth auth;
	private Dao<Task, Integer> taskDao;
	private MyLocationOverlay myLocationOverlay;
	private Geocoder geocoder;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		app = (DewApplication) getApplicationContext();
		auth = app.getAuth();
		
	    setContentView(R.layout.taskmap);
	    
	    List<Task> tasks;
		try {
			taskDao = app.getHelper().getTaskDao();
			tasks = taskDao.queryBuilder().where().eq("user_id", auth.getUser()).query();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	    
	    final MapView mapView = (MapView) findViewById(R.id.mapview);
	    mapView.setBuiltInZoomControls(true);
	    
	    final MapController mapController = mapView.getController();
	    
		List<Overlay> mapOverlays = mapView.getOverlays();
		Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
		TaskItemizedOverlay itemizedOverlay = new TaskItemizedOverlay(drawable, this);
		
		myLocationOverlay = new MyLocationOverlay(this, mapView);
		mapView.getOverlays().add(myLocationOverlay);

		myLocationOverlay.runOnFirstFix(new Runnable() {
			public void run() {
				mapView.getController().animateTo(
				  myLocationOverlay.getMyLocation());
			 	}
		});
		
		geocoder = new Geocoder(this);
		
		for (Task task: tasks) {
			if (task.getLocation() != null) {
				try {
					List<Address> addresses = geocoder.getFromLocationName(task.getLocation(), 1);
					if (addresses.size() > 0) {
					    OverlayItem overlayItem = new OverlayItem(
					    	new GeoPoint((int) (addresses.get(0).getLatitude() * 1E6), (int)(addresses.get(0).getLongitude()*1E6)),
					    	task.getName(), ((task.getDetails() == null) ? "" : (task.getDetails() + "\n\n")) + task.getLocation());
					    itemizedOverlay.addOverlay(overlayItem);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		mapOverlays.add(itemizedOverlay);
		
		/*myLocationOverlay.runOnFirstFix(new Runnable() {
            public void run() {
                mapController.setZoom(13);
                mapController.animateTo(myLocationOverlay.getMyLocation());
            }
        });*/
	}
	
	protected void onResume() {
		super.onResume();
		myLocationOverlay.enableCompass(); 
		myLocationOverlay.enableMyLocation(); 
	}
	
	protected void onPause() {
		super.onPause();
		myLocationOverlay.disableCompass();
		myLocationOverlay.disableMyLocation();
	}
	
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	class TaskItemizedOverlay extends com.google.android.maps.ItemizedOverlay<OverlayItem> {
		Context mContext;
		
		private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

		public void addOverlay(OverlayItem overlay) {
			mOverlays.add(overlay);
			populate();
		}
		
		public TaskItemizedOverlay(Drawable defaultMarker, Context context) {
			super(boundCenterBottom(defaultMarker));
			populate();
			mContext = context;
		}

		@Override
		protected OverlayItem createItem(int i) {
			return mOverlays.get(i);
		}

		@Override
		public int size() {
			return mOverlays.size();
		}
		
		protected boolean onTap(int index) {
			OverlayItem item = mOverlays.get(index);
			AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
			dialog.setTitle(item.getTitle());
			dialog.setMessage(item.getSnippet());
			dialog.show();
			return true;
		}
	}
}
